import threading
from sqlalchemy import (Column, Integer, String, Unicode, Text, UnicodeText,
Numeric, Float, DateTime, Date, Time, Interval, Boolean, Binary, PickleType)
from sqlalchemy.ext.declarative import declarative_base
import json
Base = declarative_base()

import webapp2

def get_session(app=None):
    app = app or webapp2.get_app()
    return app.dbsession 
    
class Model(Base):
    __abstract__ = True
        
    @classmethod
    def get(cls, *args):
        return get_session().query(cls).get(*args)
        
    @classmethod
    def query(cls):
        return get_session().query(cls)
        
    def put(self):
        get_session().add(self)
        get_session().commit()
    
    def __repr__(self):
       return "<%s('%s')>" % (self.__class__.__name__, self.id)

class Json(PickleType):
    impl = Text
    def __init__(self, *args, **kwargs):
        super(Json, self).__init__(pickler=json)
        
'''
class SomeOtherEntity(Base):
    __tablename__ = 'some_other_entity'
    id = Column(Integer, primary_key=True)
    attributes = Column(TextPickleType(pickler=json))
    '''
metadata = Base.metadata

if __name__ == "__main__":
    metadata.create_all(engine)
